package sanglh.edu.hsphysics.main;

import java.util.ArrayList;

import sanglh.edu.hsphysics.R;
import sanglh.edu.hsphysics.formulas.FormulaItem;
import sanglh.edu.hsphysics.utils.CommonUtils;
import sanglh.edu.hsphysics.view.FloatingActionButton;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

public class FragmentTest extends FragmentNavigationDrawerItem {
	private static final String RES_NAME = "file_page";
	private ListView mListView;

	@Override
	public String getTitle() {
		return getString(R.string.test);
	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_test, container,
				false);
		mListView = (ListView) rootView.findViewById(R.id.lv);
		loadData();
		return rootView;
	}

	private void loadData() {
		ArrayList<TestItem> items = new ArrayList<FragmentTest.TestItem>();
		items.add(new TestItem(String.format("%s %s",
				getString(R.string.test_university), "2015"), RES_NAME,
				getString(R.string.dh), 10));
		items.add(new TestItem(String.format("%s %s",
				getString(R.string.test_university), "2014"), RES_NAME,
				getString(R.string.dh), 10));
		items.add(new TestItem(String.format("%s %s",
				getString(R.string.test_university), "2013"), RES_NAME,
				getString(R.string.dh), 10));
		items.add(new TestItem(String.format("%s %s",
				getString(R.string.test_university), "2012"), RES_NAME,
				getString(R.string.dh), 10));
		items.add(new TestItem(String.format("%s %s",
				getString(R.string.test_university), "2011"), RES_NAME,
				getString(R.string.dh), 10));
		items.add(new TestItem(String.format("%s %s",
				getString(R.string.test_university), "2010"), RES_NAME,
				getString(R.string.dh), 10));

		items.add(new TestItem(String.format("%s %s",
				getString(R.string.test_national), "2015"), RES_NAME,
				getString(R.string.tn), 10));
		items.add(new TestItem(String.format("%s %s",
				getString(R.string.test_national), "2014"), RES_NAME,
				getString(R.string.tn), 10));
		items.add(new TestItem(String.format("%s %s",
				getString(R.string.test_national), "2013"), RES_NAME,
				getString(R.string.tn), 10));
		items.add(new TestItem(String.format("%s %s",
				getString(R.string.test_national), "2012"), RES_NAME,
				getString(R.string.tn), 10));
		items.add(new TestItem(String.format("%s %s",
				getString(R.string.test_national), "2011"), RES_NAME,
				getString(R.string.tn), 10));
		items.add(new TestItem(String.format("%s %s",
				getString(R.string.test_national), "2010"), RES_NAME,
				getString(R.string.tn), 10));
		TestAdapter adapter = new TestAdapter(getActivity(), items,
				R.layout.row_test);
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(this);

	}

	private class TestItem extends FormulaItem {
		public String sub;

		public TestItem(String title, String formulaResName, String sub,
				int count) {
			super(title, formulaResName, 0, count);
			this.sub = sub;
		}
	}

	static class Hoder {
		TextView tv;
		TextView sub;
		FloatingActionButton fab;
	}

	public class TestAdapter extends BaseAdapter {
		private Context context;
		private ArrayList<TestItem> items;
		private int resId;

		public TestAdapter(Context context, ArrayList<TestItem> items, int resId) {
			super();
			this.context = context;
			this.items = items;
			this.resId = resId;
		}

		@Override
		public int getCount() {
			return items.size();
		}

		@Override
		public Object getItem(int position) {
			return items.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Hoder hoder = null;
			TestItem drawerItem = items.get(position);
			if (null == convertView) {
				convertView = LayoutInflater.from(context).inflate(resId, null);
				hoder = new Hoder();
				hoder.tv = (TextView) convertView.findViewById(R.id.tv);
				hoder.sub = (TextView) convertView.findViewById(R.id.tv2);
				hoder.fab = (FloatingActionButton) convertView
						.findViewById(R.id.img);
				convertView.setTag(hoder);
			} else {
				hoder = (Hoder) convertView.getTag();
			}

			hoder.tv.setText(drawerItem.title);
			hoder.sub.setText(drawerItem.sub);
			if (position % 3 == 0) {
				hoder.fab.setColorNormal(context.getResources().getColor(
						R.color.light_blue));
			} else if (position % 2 == 0) {
				hoder.fab.setColorNormal(context.getResources().getColor(
						R.color.cyan));
			} else {
				hoder.fab.setColorNormal(context.getResources().getColor(
						R.color.teal));
			}
			if (parent instanceof ListView) {
				CommonUtils.listAnimation((ListView) parent, convertView,
						position);
			} else if (parent instanceof GridView) {
				CommonUtils.gridAnimation((GridView) parent, convertView,
						position);
			}
			return convertView;
		}

	}

}
